﻿@model Site.Models.OpenGamesDTO
@{
    ViewBag.Title = "Open games";
        if (ViewBag.Message != null)
    {
        <script> alert("@ViewBag.Message");</script>
    }
}

<h2>Open games</h2>
@{
        <ul>
        @foreach (var game in Model.Games)
        {
            <li>
                @String.Format("Id: {0} Players: {1}", game.Id, game.Players.Count)
                @if (game.Players.All(p => p.Name != Convert.ToString(Session["Username"])))
                {
                   @Html.ActionLink("Join game", "JoinGame", new {gameId = game.Id})
                }
                @if ( Model.IsPlayerCreatorOfGame(game.Id) && game.Players.Count > 1 )
                {
                    @Html.ActionLink("Start game", "StartGame", new {gameId = game.Id})
                }
                <ul>
                    <li>Players:
                        <ul>
                        @foreach (var player in game.Players)
                        {
                            <li>
                                @player.Name
                            </li>  
                        }
                        
                        </ul>
                    </li>
                    
                </ul>
            </li>
        }

        </ul>
        <p>@Html.ActionLink("Create new game", "CreateGame", "OpenGames")</p>
    <p> Return to @Html.ActionLink("Home", "Index", "Home")</p>
}